#version 320 es

precision mediump int;
precision highp float;

out vec4 FragColor;
in vec2 TexCoord;

//是否使用rgb，如果为false就使用yuv
uniform bool use_rgb;

// rgb
uniform sampler2D tex_rgb;

// yuv
uniform sampler2D tex_y;
uniform sampler2D tex_u;
uniform sampler2D tex_v;

// 蒙版纹理
uniform sampler2D tex_mask;

void main(void)
{
    if(use_rgb)
    {
        FragColor = texture(tex_rgb, TexCoord);
    }
    else{
        vec3 yuv;
        vec3 rgb;

        yuv.x = texture(tex_y, TexCoord).r;
        yuv.y = texture(tex_u, TexCoord).r - 0.5;
        yuv.z = texture(tex_v, TexCoord).r - 0.5;
        rgb = mat3( 1,       1,         1,
                    0,       -0.39465,  2.03211,
                    1.13983, -0.58060,  0) * yuv;

        // 蒙版
        vec4 mask_rgb = texture(tex_mask, TexCoord);
        bool mask_avlid = mask_rgb.x != 0.0 && mask_rgb.y!= 0.0 && mask_rgb.z != 0.0;
        if(mask_avlid){
            FragColor = mask_rgb;
        }else{
            FragColor = vec4(rgb, 1.0f);
        }
    }
}
